<template>
  <div>
    <a-modal
      centered
      :title="title"
      :width="1200"
      :confirmLoading="confirmLoading"
      :visible="visible"
      @ok="handleOk"
      @cancel="handleCancel"
      cancelText="关闭"
    >
      <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="区域名称"
          :hidden="false"
          hasFeedback >
          <a-input id="areaName" placeholder="请输入区域名称" v-decorator="['areaName', validatorRules.name ]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="备注"
          :hidden="false"
          hasFeedback >
          <a-input placeholder="请输入备注" v-decorator="['remark']"/>
        </a-form-item>
      </a-form>
      </a-spin>
      <!-- table区域-end -->
    </a-modal>
  </div>
</template>

<script>
import { filterObj } from '@/utils/util'
import { getAction,postAction } from '@/api/manage'
import { duplicateAreaCheck, duplicateCheck } from '@api/api'
import pick from 'lodash.pick'

export default {
  name: 'selectProjectModel',
  data() {
    return {
      title: '添加项目区域以及绑定项目',
      parentId:"",
      names: [],
      confirmLoading: false,
      formVisible:true,
      //区域id
      areaId:"",
      visible: false,
      placement: 'right',
      description: '',
      form: this.$form.createForm(this),
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },

      validatorRules:{
        name: {
          rules: [{
            required: true, message: '请输入区域名称!'
          }, {
            validator: this.validateAreaName,
          }]
        },
      },
      node:{},
      model:{},
      loading: false,
      selectedRowKeys: [],
      selectedRows: [],
      url: {
        bind: '/area/bind'
      },
      parentNode:{}
    }
  },
  created() {
  },
  methods: {
    init() {
      this.queryParam = {}
      this.dataSource1 = []
      this.dataSource2 = []
      this.selectedRowKeys = []
      this.selectedRows = []
      this.searchReset()
    },
    searchQuery() {
      this.loadData(1)
    },
    searchReset() {
      this.queryParam = {}
      this.loadData(1)
    },
    handleCancel() {
      this.visible = false
    },
    validateAreaName(rule, value, callback) {
      var params = {
        parentId:this.parentId,
        name: value,
      };
      duplicateAreaCheck(params).then((res) => {
        if (res.success) {
          callback()
        } else {
          callback("区域已存在!")
        }
      })
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
            let formData = Object.assign(this.model, values);
            formData.parentId = this.parentId
            formData.parentType = this.parentNode.isArea
            postAction(this.url.bind, formData).then((res) => {
              if(res.success){
                this.form.resetFields();
                this.$message.success(res.message)
                this.visible=false
                this.$emit('ok',this.node)
                this.loadData()
                this.confirmLoading=true
              }else{
                this.$message.warning("系统内部错误")
              }
            })
        }
      })


      //子组件向父组件传值
      // this.$emit('selectFinished', this.dataSource2)
      //绑定项目


      this.visible = false
    },
    setAreaId(node){
      this.node = node
      this.visible = true
      this.parentId = node.key
      this.parentNode = node
    },
  }
}
</script>
<style lang="less" scoped>
.ant-card-body .table-operator {
  margin-bottom: 18px;
}

.ant-table-tbody .ant-table-row td {
  padding-top: 15px;
  padding-bottom: 15px;
}

.anty-row-operator button {
  margin: 0 5px;
}

.ant-btn-danger {
  background-color: #ffffff;
}

.ant-modal-cust-warp {
  height: 100%;
}

.ant-modal-cust-warp .ant-modal-body {
  height: calc(100% - 110px) !important;
  overflow-y: auto;
}

.ant-modal-cust-warp .ant-modal-content {
  height: 90% !important;
  overflow-y: hidden;
}
</style>